/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import entity.Employee;
import entity.PlainTextMessage;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.event.ActionEvent;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import manager.MessageMeManager;
import session.stateless.EmployeeBeanLocal;
import session.stateless.MessageMeSessionLocal;

/**
 *
 * @author Vincent
 */
@ManagedBean(name="messageMeManagerBean")
@ViewScoped
public class MessageMeManagerBean implements Serializable{
    
    @EJB 
    MessageMeSessionLocal messageMeSessionLocal; 
    @EJB
    EmployeeBeanLocal employeeSessionBeanLocal;
    
    private MessageMeManager messageMeManager;
    private List<PlainTextMessage> messages;
    private String statusMessage;
    private String messageText;
    private String title;
    private String replyTitle;
    private String replyMessage;
    private String sender;
    private String receiver;
    private Employee employee;
    private PlainTextMessage selectedMessage;
    
    public List<PlainTextMessage> getMessages() {
        return messages;
    }

    public void setMessages(List<PlainTextMessage> messages) {
        this.messages = messages;
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public PlainTextMessage getSelectedMessage() {
        return selectedMessage;
    }

    public void setSelectedMessage(PlainTextMessage selectedMessage) {
        this.selectedMessage = selectedMessage;
    }

    public String getReplyMessage() {
        return replyMessage;
    }

    public void setReplyMessage(String replyMessage) {
        this.replyMessage = replyMessage;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getReplyTitle() {
        return replyTitle;
    }

    public void setReplyTitle(String replyTitle) {
        this.replyTitle = replyTitle;
    }
    
    
        /** Creates a new instance of MessageMeManagerBean */
    public MessageMeManagerBean() {
    }
    
    public void initView(PhaseEvent event){
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        employee = controller.getEmployee();
        messageMeManager = new MessageMeManager(messageMeSessionLocal); 
        messages = messageMeManager.getAllMessages(employee.getUserName());
    }
    
    public void refresh(ActionEvent event){
         messageMeManager = new MessageMeManager(messageMeSessionLocal); 
         messages = messageMeManager.getAllMessages(employee.getUserName());
         statusMessage = "Refresh Successfully."; 

         FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Refresh Result: " + statusMessage, "")); 
    }
    
    public void send(ActionEvent event){
        sender = employee.getUserName();
        
         System.out.println("userName " + sender);
         
         receiver = String.valueOf(receiver);
         System.out.println("receiver " + receiver);
         
         messageMeManager = new MessageMeManager(messageMeSessionLocal); 
         messageMeManager.messageMe(this.messageText, sender, receiver, title);
         
         sender="";
         receiver="";
         title="";
         messageText="";
         statusMessage = "Send Successfully."; 

         FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Refresh Result: " + statusMessage, "")); 
    }
    
    public void reply(ActionEvent event)
    {
        String temp_replyMessage = String.valueOf(replyMessage);
        String temp_title = String.valueOf(replyTitle);
        String sender = selectedMessage.getReceiver();
        String receiver = selectedMessage.getSender();
         messageMeManager = new MessageMeManager(messageMeSessionLocal); 
         messageMeManager.messageMe(temp_replyMessage, sender, receiver, temp_title);
         statusMessage = "Save Successfully."; 

         FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Refresh Result: " + statusMessage, "")); 
         replyTitle = "";
         replyMessage = "";
    }
    
    public void deleteMessage(ActionEvent event) throws IOException
    {
        try
        {
             messageMeManager = new MessageMeManager(messageMeSessionLocal); 
             messageMeManager.deleteMessage(selectedMessage.getPlainTextMessageId());
             selectedMessage = null;
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<Employee> list = employeeSessionBeanLocal.getEmployees();
        
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getUserName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getUserName());
        }
        return results;  
    }
    

}
